package neusoft.example.com.yuejian06.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.example.com.yuejian06.Adapter.PictureCommentListAdapter;
import neusoft.example.com.yuejian06.Bean.Comment;
import neusoft.example.com.yuejian06.Bean.Picture;
import neusoft.example.com.yuejian06.Fragment.MyDividerItemDecoration;
import neusoft.example.com.yuejian06.Fragment.PictureListFragment;
import neusoft.example.com.yuejian06.Model.PictureCommentListModel;
import neusoft.example.com.yuejian06.Model.PictureModel;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

public class PictureDetailActivity extends AppCompatActivity {
    private Intent intent;
    private String user_id;
    private ImageView user_pic;
    private TextView user_name;
    private String picture_id;
    private TextView picture_title;
    private TextView picture_time;
    private TextView picture_content;
    private ImageView picture_pic,shaitu_return;
    private TextView comment_content;
    private TextView comment_time;

    private Button comment;
    private PictureCommentListAdapter adapter;
    private RetrofitListener<List<Comment>> listener;
    private RetrofitListener<Picture> picturelistener;
    private List<Comment> list;
    private RecyclerView recyclerView;
    private PictureModel pictureModel;
    private PictureCommentListModel commentListModel;
    private LinearLayoutManager layoutManager;

    private PublicActivity publicActivity;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_detail);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();

        sp = getSharedPreferences("user_id", MODE_PRIVATE);
        user_id = sp.getString("user_id", null);
        picture_id = getIntent().getStringExtra("picture_id");

        adapter = new PictureCommentListAdapter(PictureDetailActivity.this, list);
        recyclerView = (RecyclerView) findViewById(R.id.picture_rec);


        layoutManager = new LinearLayoutManager(PictureDetailActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new MyDividerItemDecoration(this,LinearLayoutManager.VERTICAL));



        picturelistener = new RetrofitListener<Picture>() {
          PublicActivity publicActivity=new PublicActivity();
            @Override
            public void onResponse(Picture l) {

                Picasso.with(PictureDetailActivity.this)
                        .load(publicActivity.getIp()+"static/picture/"+l.getPicture_pic())
                        .into(picture_pic);

                Picasso.with(PictureDetailActivity.this)
                        .load(publicActivity.getIp()+"static/user_picture/"+l.getUser_pic())
                        .into(user_pic);


                user_name.setText(l.getUser_name());
                picture_time.setText(l.getPicture_time());
                //picture_title.setText("#"+l.getPicture_title()+"#");
                picture_content.setText("#"+l.getPicture_title()+"#"+l.getPicture_content());
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(PictureDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };




        listener = new RetrofitListener<List<Comment>>() {
            @Override
            public void onResponse(List<Comment> l) {
                list = l;
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(PictureDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };

        commentListModel = new PictureCommentListModel();
        commentListModel.getCommentList(picture_id, listener);
        pictureModel = new PictureModel();
        pictureModel .getPicture(picture_id, picturelistener);

        shaitu_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent=new Intent(PictureDetailActivity.this, PictureListFragment.class);
                startActivity(intent);
            }
        });


    }
    private void initView(){
        user_pic = (ImageView) findViewById(R.id.user_pic);
        user_name = (TextView) findViewById(R.id.user_name);
        shaitu_return=(ImageView)findViewById(R.id.shaitu_return);
        picture_pic = (ImageView) findViewById(R.id.picture_pic);
        picture_title = (TextView) findViewById(R.id.picture_title);
        picture_time = (TextView) findViewById(R.id.picture_time);
        picture_content = (TextView) findViewById(R.id.picture_content);
        comment_content=(TextView)findViewById(R.id.comment_content);
        comment_time=(TextView)findViewById(R.id.comment_time);
    }

}
